<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Sunny, Moon, Cloudy, Lightning, Umbrella, WindPower, Opportunity, Sunrise, Sunset } from '@element-plus/icons-vue'
import http from '@/utils/http'

interface WeatherData {
  city: string
  date: string
  temperature: number
  weather: string
  humidity: number
  windSpeed: number
  windDirection: string
  airQuality: {
    index: number
    level: string
    advice: string
  }
  sunrise: string
  sunset: string
  forecast: Array<{
    date: string
    dayOfWeek: string
    weather: string
    tempHigh: number
    tempLow: number
    humidity: number
  }>
  healthTips: Array<string>
}

// 模拟天气数据，实际项目中应通过API获取
const weatherData = ref<WeatherData>({
  city: '北京',
  date: '2023年11月15日',
  temperature: 12,
  weather: '晴',
  humidity: 45,
  windSpeed: 3.2,
  windDirection: '东北风',
  airQuality: {
    index: 75,
    level: '良',
    advice: '空气质量可接受，但某些污染物可能对极少数异常敏感人群健康有较弱影响'
  },
  sunrise: '06:58',
  sunset: '17:02',
  forecast: [
    { date: '11月16日', dayOfWeek: '周四', weather: '多云', tempHigh: 14, tempLow: 5, humidity: 50 },
    { date: '11月17日', dayOfWeek: '周五', weather: '晴', tempHigh: 15, tempLow: 4, humidity: 45 },
    { date: '11月18日', dayOfWeek: '周六', weather: '小雨', tempHigh: 10, tempLow: 3, humidity: 80 },
    { date: '11月19日', dayOfWeek: '周日', weather: '阴', tempHigh: 9, tempLow: 2, humidity: 65 },
    { date: '11月20日', dayOfWeek: '周一', weather: '晴', tempHigh: 11, tempLow: 1, humidity: 40 }
  ],
  healthTips: [
    '今日紫外线强度较高，外出请做好防晒措施',
    '温差较大，注意适时增减衣物，预防感冒',
    '空气质量良好，适合户外活动',
    '花粉浓度中等，过敏人群需注意防护'
  ]
})

// 获取天气图标
const getWeatherIcon = (weather: string) => {
  switch (weather) {
    case '晴':
      return Sunny
    case '多云':
      return Cloudy
    case '阴':
      return Moon
    case '小雨':
    case '中雨':
    case '大雨':
      return Umbrella
    case '雷阵雨':
      return Lightning
    default:
      return Sunny
  }
}

// 获取空气质量等级对应的颜色
const getAirQualityColor = (level: string) => {
  switch (level) {
    case '优':
      return '#8cc152'
    case '良':
      return '#f6bb42'
    case '轻度污染':
      return '#e9573f'
    case '中度污染':
      return '#da4453'
    case '重度污染':
      return '#967adc'
    case '严重污染':
      return '#434a54'
    default:
      return '#f6bb42'
  }
}

// 实际项目中应通过API获取天气数据
// const fetchWeatherData = async () => {
//   try {
//     const response = await http.get('/api/weather')
//     weatherData.value = response.data
//   } catch (error) {
//     console.error('获取天气数据失败:', error)
//   }
// }

onMounted(() => {
  // fetchWeatherData()
})
</script>

<template>
  <div class="weather-monitor-container">
    <el-card class="weather-card main-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <h2>天气监控</h2>
          <span class="location">{{ weatherData.city }} · {{ weatherData.date }}</span>
        </div>
      </template>

      <div class="current-weather">
        <div class="weather-info">
          <div class="temperature">{{ weatherData.temperature }}<span class="degree">°C</span></div>
          <div class="weather-desc">
            <el-icon :size="32"><component :is="getWeatherIcon(weatherData.weather)" /></el-icon>
            <span>{{ weatherData.weather }}</span>
          </div>
        </div>

        <div class="weather-details">
          <div class="detail-item">
            <el-icon><wind-power /></el-icon>
            <span>风速: {{ weatherData.windSpeed }}m/s</span>
          </div>
          <div class="detail-item">
            <el-icon><opportunity /></el-icon>
            <span>湿度: {{ weatherData.humidity }}%</span>
          </div>
          <div class="detail-item">
            <el-icon><sunrise /></el-icon>
            <span>日出: {{ weatherData.sunrise }}</span>
          </div>
          <div class="detail-item">
            <el-icon><sunset /></el-icon>
            <span>日落: {{ weatherData.sunset }}</span>
          </div>
        </div>
      </div>

      <el-divider content-position="center">空气质量</el-divider>

      <div class="air-quality">
        <el-progress
          type="dashboard"
          :percentage="weatherData.airQuality.index"
          :color="getAirQualityColor(weatherData.airQuality.level)"
          :stroke-width="6"
        >
          <template #default>
            <div class="air-quality-info">
              <div class="air-index">{{ weatherData.airQuality.index }}</div>
              <div class="air-level">{{ weatherData.airQuality.level }}</div>
            </div>
          </template>
        </el-progress>
        <div class="air-advice">
          <p>{{ weatherData.airQuality.advice }}</p>
        </div>
      </div>
    </el-card>

    <el-card class="weather-card forecast-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <h3>未来天气预报</h3>
        </div>
      </template>

      <div class="forecast-list">
        <div v-for="(day, index) in weatherData.forecast" :key="index" class="forecast-item">
          <div class="forecast-date">
            <div class="day">{{ day.dayOfWeek }}</div>
            <div class="date">{{ day.date }}</div>
          </div>
          <div class="forecast-icon">
            <el-icon :size="24"><component :is="getWeatherIcon(day.weather)" /></el-icon>
          </div>
          <div class="forecast-weather">{{ day.weather }}</div>
          <div class="forecast-temp">
            <span class="high">{{ day.tempHigh }}°</span>
            <span class="low">{{ day.tempLow }}°</span>
          </div>
        </div>
      </div>
    </el-card>

    <el-card class="weather-card health-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <h3>健康提示</h3>
        </div>
      </template>

      <div class="health-tips">
        <el-tag
          v-for="(tip, index) in weatherData.healthTips"
          :key="index"
          class="health-tip-tag"
          effect="light"
          round
        >
          {{ tip }}
        </el-tag>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.weather-monitor-container {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  padding: 20px;
}

@media (min-width: 1200px) {
  .weather-monitor-container {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "main forecast"
      "health health";
  }

  .main-card {
    grid-area: main;
  }

  .forecast-card {
    grid-area: forecast;
  }

  .health-card {
    grid-area: health;
  }
}

.weather-card {
  border-radius: 12px;
  overflow: hidden;
  transition: transform 0.3s ease;
}

.weather-card:hover {
  transform: translateY(-5px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2, .card-header h3 {
  margin: 0;
  font-weight: 600;
  color: #303133;
}

.location {
  color: #909399;
  font-size: 14px;
}

.current-weather {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 10px 0;
}

@media (min-width: 768px) {
  .current-weather {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }
}

.weather-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.temperature {
  font-size: 48px;
  font-weight: 700;
  color: #303133;
  line-height: 1;
}

.degree {
  font-size: 24px;
  font-weight: 400;
  vertical-align: top;
}

.weather-desc {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  font-size: 18px;
  color: #606266;
}

.weather-details {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}

.detail-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #606266;
}

.air-quality {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 10px 0;
}

@media (min-width: 768px) {
  .air-quality {
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
  }
}

.air-quality-info {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.air-index {
  font-size: 24px;
  font-weight: 700;
  color: #303133;
}

.air-level {
  font-size: 14px;
  color: #606266;
}

.air-advice {
  max-width: 400px;
  text-align: center;
  color: #606266;
  font-size: 14px;
}

.forecast-list {
  display: flex;
  overflow-x: auto;
  gap: 15px;
  padding: 10px 0;
}

.forecast-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 80px;
  padding: 10px;
  border-radius: 8px;
  background-color: #f5f7fa;
  transition: background-color 0.3s ease;
}

.forecast-item:hover {
  background-color: #ecf5ff;
}

.forecast-date {
  text-align: center;
  margin-bottom: 10px;
}

.day {
  font-weight: 600;
  color: #303133;
}

.date {
  font-size: 12px;
  color: #909399;
}

.forecast-icon {
  margin: 5px 0;
}

.forecast-weather {
  font-size: 14px;
  color: #606266;
  margin: 5px 0;
}

.forecast-temp {
  display: flex;
  gap: 8px;
}

.high {
  color: #f56c6c;
  font-weight: 600;
}

.low {
  color: #409eff;
  font-weight: 600;
}

.health-tips {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px 0;
}

.health-tip-tag {
  margin: 5px;
  padding: 8px 12px;
}
</style>